package com.feeyo.groundservice.composewidget.view.image

import android.os.Build
import android.view.View
import android.widget.ImageView
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import coil.ImageLoader
import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import com.blankj.utilcode.util.ActivityUtils
import com.feeyo.groundservice.composewidget.R
import com.feeyo.groundservice.composewidget.util.intervalClick
import com.feeyo.groundservice.base_util.showImage
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader

/**
 *作者：QLH on 2023-07-26
 *描述：
 */
/**自定义的Image，可以加载gif；可以点击进行预览
 *
 *
 * @param preViewBigImg 是否预览的是大图
 * @param preErrorImgRes 预览失败的图片
 * **/
@Composable
fun CustomAsyncImage(
    model: Any?,
    modifier: Modifier = Modifier,
    contentDescription: String?,
    imageLoader: ImageLoader? = null,
    placeholder: Painter? = null,
    error: Painter? = null,
//    fallback: Painter? = error,
//    onLoading: ((AsyncImagePainter.State.Loading) -> Unit)? = null,
//    onSuccess: ((AsyncImagePainter.State.Success) -> Unit)? = null,
//    onError: ((AsyncImagePainter.State.Error) -> Unit)? = null,
//    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.Fit,
//    alpha: Float = DefaultAlpha,
//    colorFilter: ColorFilter? = null,
//    filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
    currentPosition: Int? = 0,//支持预览滚动
    images: List<String?> = arrayListOf(),
    preViewBigImg: Boolean = false,
    preErrorImgRes: Int? = null
) {

    Box(modifier = modifier) {
        var img: ImageView? = null
        val imgLoader = imageLoader ?: ImageLoader.Builder(LocalContext.current)
            .components {
                //构建GIF解码器
                if (Build.VERSION.SDK_INT >= 28)
                    add(ImageDecoderDecoder.Factory())
                else
                    add(GifDecoder.Factory())
            }
            .build()
        //图像
        AsyncImage(
            model = model,
            contentDescription = contentDescription,
            modifier = Modifier.fillMaxSize().intervalClick {
                //支持多张预览
                if (currentPosition != null && images.isNotEmpty()) {
                    //大图预览
                    XPopup.Builder(ActivityUtils.getTopActivity())
                        .asImageViewer(
                            img!!,
                            currentPosition,
                            images,
                            null,
                            SmartGlideImageLoader()
                        )
                        .isShowSaveButton(true)
                        .show()
                } else {
                    //大图预览
                    XPopup.Builder(ActivityUtils.getTopActivity())
                        .asImageViewer(
                            img!!,
                            model,
                            SmartGlideImageLoader(
                                preViewBigImg,
                                preErrorImgRes ?: R.drawable.icon_placeholder_error
                            )
                        )
                        .isShowSaveButton(true)
                        .show()
                }

            },
            placeholder = placeholder ?: rememberAsyncImagePainter(
                model = R.drawable.icon_placeholder_loading,
                imageLoader = imgLoader,//配置GIF图片加载器
                contentScale = contentScale,
            ),
            error = error ?: rememberAsyncImagePainter(
                model = R.drawable.icon_placeholder_error,
                contentScale = contentScale,
            ),
            contentScale = contentScale,
        )

        //原生中间View，为了预览而设置的
        AndroidView(
            factory = {
                ImageView(it)
            },
            modifier = Modifier.fillMaxSize(),
            update = {
                img = it.apply {
                    scaleType = ImageView.ScaleType.CENTER_CROP
                    visibility = View.INVISIBLE
                    showImage(model)
                }

            }
        )
    }

}

/**自定义的Image，可以加载gif**/
@Composable
fun CustomAsyncImage(
    model: Any?,
    modifier: Modifier = Modifier,
    contentDescription: String?,
    imageLoader: ImageLoader? = null,
    placeholder: Painter? = null,
    error: Painter? = null,
    contentScale: ContentScale = ContentScale.Fit,
) {
    val imgLoader = imageLoader ?: ImageLoader.Builder(LocalContext.current)
        .components {
            //构建GIF解码器
            if (Build.VERSION.SDK_INT >= 28)
                add(ImageDecoderDecoder.Factory())
            else
                add(GifDecoder.Factory())
        }
        .build()
    //图像
    AsyncImage(
        model = model,
        contentDescription = contentDescription,
        modifier = modifier,
        placeholder = placeholder ?: rememberAsyncImagePainter(
            model = R.drawable.icon_placeholder_loading,
            imageLoader = imgLoader,//配置GIF图片加载器
            contentScale = contentScale,
        ),
        error = error ?: rememberAsyncImagePainter(
            model = R.drawable.icon_placeholder_error,
            contentScale = contentScale,
        ),
        contentScale = contentScale,
    )
}